﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MVBuddy.Common.Model
{
    public class NotificationData
    {
        public int NotificationCount { get; set; }
        public int FavoriteCount { get; set; }
        public int MessageCount { get; set; }

        public NotificationData()
        {
            this.NotificationCount = 0;
            this.FavoriteCount = 0;
            this.MessageCount = 0;
        }

        public static NotificationData FromHtmlDocument(HtmlDocument document)
        {
            NotificationData data = null;
            HtmlNode userInfo = document.DocumentNode.Descendants().Where(child => child.Id == "userinfo").FirstOrDefault();

            if (!userInfo.InnerText.Contains("Iniciar"))
            {
                data = new NotificationData();
                List<HtmlNode> aNodes = userInfo.ChildNodes.Where(child => child.Name == "a").ToList();

                data.NotificationCount = ExtractCountField(aNodes[1]);
                data.FavoriteCount = ExtractCountField(aNodes[2]);
                data.MessageCount = ExtractCountField(aNodes[3]);
            }

            return data;
        }

        private static int ExtractCountField(HtmlNode node)
        {
            if (node.FirstChild.Name == "strong")
            {
                return int.Parse(node.FirstChild.InnerText);
            }

            return 0;
        }
    }
}
